﻿using Iot.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Primitives;
using System.Data;
using System.Reflection;
using System.Security.Claims;

namespace Iot.Auth
{
    public class AdminRequirementHandler : AuthorizationHandler<AdminRequirement>
    {
        public AdminRequirementHandler()
        {
        }

        protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, AdminRequirement requirement)
        {
            
            if (context.Resource is Microsoft.AspNetCore.Components.RouteData)
            {
                if (context.User.Identity?.IsAuthenticated == true)
                    context.Succeed(requirement);
            }
            else
            {
                context.Succeed(requirement);
            }
            //context.Succeed(requirement);
            //if (context.User.Identity?.IsAuthenticated != true)
            //    return Task.CompletedTask ;
            
            //var nameIdentifier = context.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            ////判断是否为超级账号
            //if (nameIdentifier == AppData.AppConfig.Username)
            //    context.Succeed(requirement);

            return Task.CompletedTask;
        }
    }
}
